package com.lgywin.runserver.service;

import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import com.lgywin.runserver.comm.DateUtils;
import com.lgywin.runserver.comm.Global;
import com.lgywin.runserver.dao.UserMapper;
import com.lgywin.runserver.entity.User;
import com.lgywin.runserver.entity.generate.NetCode;
import com.lgywin.runserver.entity.generate.RetCode;
import com.lgywin.runserver.protobuf.LoginProto;
import com.lgywin.runserver.protobuf.MsgProto;
import com.lgywin.runserver.protobuf.NetProto;
import com.lgywin.runserver.protobuf.RegProto;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class UserService {
    @Resource
    private UserMapper userDao;

    @Transactional
    public void insert(User user) {
        userDao.insert(user);
    }

    public User findUser(User user){
        return userDao.findUser(user);
    }

    public User findIp(User user){return userDao.findIp(user);}

    @Transactional
    public NetProto login(ChannelHandlerContext ctx, NetProto netProto) throws InvalidProtocolBufferException {
        LoginProto bean = netProto.getCls(0).unpack(LoginProto.class);
        log.info("登录:" + bean.getUsername() + "=" + bean.getPassword());

        User find = new User();
        find.setUsername(bean.getUsername());
        find.setPassword(bean.getPassword());
        User user = findUser(find);
        //匹配 区服ID
        //是否存在区服ID 如果不存在给一个默认的区服ID

        //匹配 玩家ID

        MsgProto.Builder msgProto = MsgProto.newBuilder();
        msgProto.setProtoCode(NetCode.LOGINPROTO);
        if (user != null && StringUtils.isNotBlank(user.getUsername())) {
            msgProto.setCode(0);
        } else {
            msgProto.setCode(1);
        }

        NetProto netProto2 = netProto.toBuilder().setProtoCode(NetCode.MSGPROTO).setCls(0, Any.pack(msgProto.build())).build();
        return netProto2;
    }

    @Transactional
    public NetProto reg(ChannelHandlerContext ctx, NetProto netProto) throws InvalidProtocolBufferException {
        Date curDate = new Date();

        RegProto bean = netProto.getCls(0).unpack(RegProto.class);
        String ip = ctx.channel().remoteAddress().toString();
        User findIp = new User();
        findIp.setIp(ip);
        Date lastVistorTime = DateUtils.sub(curDate, Global.RegTimeLimit);

        findIp.setLastVistorTime(lastVistorTime);
        User retIp = findIp(findIp);
        if(retIp != null && retIp.getId() != null){
            MsgProto.Builder msgProto = MsgProto.newBuilder();
            msgProto.setProtoCode(NetCode.REGPROTO);
            msgProto.setCode(RetCode.LOGIN_ERROR_REP);
            NetProto netProto2 = netProto.toBuilder().setProtoCode(NetCode.MSGPROTO).setCls(0, Any.pack(msgProto.build())).build();
            return netProto2;
        }

        User find = new User();
        find.setUsername(bean.getUsername());
        find.setPassword(null);
        User user = findUser(find);

        MsgProto.Builder msgProto = MsgProto.newBuilder();
        msgProto.setProtoCode(NetCode.REGPROTO);
        if (user == null || StringUtils.isBlank(user.getUsername())) {
            User insertBean = new User();
            insertBean.setUsername(bean.getUsername());
            insertBean.setPassword(bean.getPassword());
            insertBean.setCreateTime(curDate);
            insertBean.setLastVistorTime(curDate);
            insertBean.setIp(ip);
            insert(insertBean);
            msgProto.setCode(0);
        } else {
            msgProto.setCode(2);
        }

        NetProto netProto2 = netProto.toBuilder().setProtoCode(NetCode.MSGPROTO).setCls(0, Any.pack(msgProto.build())).build();
        return netProto2;
    }
}

